package ead.cs.service.impl;

import ead.cs.domain.Permission;
import ead.cs.domain.Role;
import ead.cs.domain.User;
import ead.cs.repository.UserRepository;
import ead.cs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public void save(User user) {
        if (user.getRole() == null || user.getRole().getRoleType() == null) {
            //throw an exception
            return;
        }
        Role role = user.getRole();
        role.setName(role.getRoleType().name());
        role.setPermissions(Permission.getPermissionsByRoleType(role.getRoleType()));
        userRepository.save(user);
    }

    @Override
    public User findOneByEmail(String email) {
        return userRepository.findOneByEmail(email);
    }
}
